const axios = require('axios');
const whiteList = require('./whiteList');

const api = {};
const baseUrl = '/api'
const instance = axios.create({
  timeout: 30000,
  baseURL: baseUrl,
})

// request拦截器
instance.interceptors.request.use(config => {

  return config
}, error => {
  Promise.reject(error)
})
//response拦截器
instance.interceptors.response.use(response => {
  const res = response.data;
  //白名单
  for (let i = 0; i < whiteList.length; i++) {
    if (response.config.url.indexOf(whiteList[i]) > -1) {
      return response.data;
    }
  }
  return response.data
}, error => {
  return Promise.reject(error)
});
//下载基础文件资源
api.getFastStartResource = (data) => {
  let url = '/download/faststart/fileload'
  return instance({
    method: 'get',
    url: url,
    params: data,
    responseType: 'blob'
  })
}

//获取文件夹列表
api.getFileNameList = (data) => {
  let url = '/download/faststart/filelist'
  return instance({
    method: 'get',
    url,
    params: data
  })
}
//获取用户信息
api.getUserInfo = (axios, data) => {
  let url = baseUrl + '/user/getUserInfo'
  return axios.get(url, {
    params: data
  })
}
//用户密码登录
api.loginWithPassword = (axios, data) => {
  let url = baseUrl + '/user/password/signin'
  return axios.post(url, data)
};
//手机号验证码登录
api.loginWithPhone = (axios, data) => {
  let url = baseUrl + '/user/phone/signin'
  return axios.post(url, data)
};
//获取二维码
api.getCaptcha = (axios, data) => {
  let url = baseUrl + '/captcha/getCaptcha'
  return axios.get(url)
};
//获取短信验证码 
api.getSmsCode = (axios, data) => {
  let url = baseUrl + '/captcha/getSmsCode'
  return axios.get(url)
}
//注册用户
api.onRegister = (axios, data) => {
  let url = baseUrl + '/user/signup'
  return axios.post(url, data);
}
export default api
